﻿@namespace BlazorFluentUI.Lists
@inherits FluentUIComponentBase
@typeparam TItem

<div class=@($"ms-DetailsList {ClassName}")
     style=@($"{Style}")
     aria-label=@AriaLabel
     @ref=@RootElementReference>
    <div role="grid"
         aria-label=@AriaLabel
         aria-readonly=true
         style="display:flex;flex-direction:column;">
        <div role="presentation"
             class="ms-DetailsList-headerWrapper">
            @if (IsHeaderVisible)
            {
                @if (HeaderTemplate != null)
                {
                    @HeaderTemplate
                }
                else
                {
                    <DetailsHeader Columns=@_adjustedColumns
                                   TItem="TItem"
                                   Selection=@Selection
                                   LayoutMode=@LayoutMode
                                   AriaLabel="ariaLabelForListHeader"
                                   AriaLabelForSelectAllCheckbox="ariaLabelForSelectAllCheckbox"
                                   AriaLabelForToggleAllGroup="ariaLabelForSelectionColumn"
                                   OnColumnAutoResized=@OnColumnAutoResized
                                   OnColumnResized=@OnColumnResizedInternal
                                   CheckboxVisibility=@CheckboxVisibility
                                   SelectAllVisibility=@selectAllVisibility />
                }
            }
        </div>
        <div role="presentation"
             class="ms-DetailsList-contentWrapper"
             style="overflow-y:hidden;height:100%;">
            <FocusZone ClassName="ms-DetailsList-focusZone"
                       Direction="FocusZoneDirection.Vertical"
                       InnerZoneKeystrokeTriggers="new System.Collections.Generic.List<ConsoleKey> { ConsoleKey.RightArrow }">
                @if (!DisableSelectionZone)
                {
                    <SelectionZone Selection=@Selection
                                   @ref="selectionZone"
                                   DisableRenderOnSelectionChanged="true"
                                   SelectionPreservedOnEmptyClick=@SelectionPreservedOnEmptyClick
                                   SelectionMode=@SelectionMode
                                   EnterModalOnTouch=@EnterModalSelectionOnTouch
                                   OnItemInvoked=@((item,index) => this.OnItemInvoked.InvokeAsync(item))>
                        @if (SubGroupSelector == null)
                        {
                            <List ItemsSource=@ItemsSource
                                  IsVirtualizing=@IsVirtualizing>
                                <ItemTemplate>
                                    @if (RowTemplate != null)
                                    {
                                        @RowTemplate(context);
                                    }
                                    else
                                    {
                                        <DetailsRow Item=@context.Item
                                                    ItemIndex=@context.Index
                                                    Columns=@_adjustedColumns
                                                    SelectionMode=@SelectionMode
                                                    Selection=@(Selection!)
                                                    Compact=@Compact
                                                    CheckboxVisibility=@CheckboxVisibility />
                                    }
                                </ItemTemplate>
                            </List>
                        }
                        else
                        {
                            <GroupedList ItemsSource=@ItemsSource
                                         TItem="TItem"
                                         TKey="object"
                                         @ref=@groupedList
                                         GetKey=@(GetKey!)
                                         Selection=@Selection
                                         SubGroupSelector=@SubGroupSelector
                                         GroupTitleSelector=@(GroupTitleSelector!)
                                         IsVirtualizing=@IsVirtualizing
                                         SelectionMode=@(CheckboxVisibility != CheckboxVisibility.Hidden ? SelectionMode : SelectionMode.None)>
                                <ItemTemplate>
                                    <DetailsRow Item=@context.Item!.Item
                                                TItem="TItem"
                                                ItemIndex=@context.Index
                                                Selection=@Selection
                                                Columns=@_adjustedColumns
                                                SelectionMode=@(CheckboxVisibility != CheckboxVisibility.Hidden ? SelectionMode : SelectionMode.None)
                                                CheckboxVisibility=@CheckboxVisibility
                                                Compact=@Compact />
                                </ItemTemplate>
                            </GroupedList>
                        }
                    </SelectionZone>

                }
                else
                {
                    @if (SubGroupSelector == null)
                    {
                        <List ItemsSource=@ItemsSource>
                            <ItemTemplate>
                                @if (RowTemplate != null)
                                {
                                    @RowTemplate(context);
                                }
                                else
                                {
                                    <DetailsRow Item=@context.Item
                                                TItem="TItem"
                                                Columns=@_adjustedColumns
                                                SelectionMode=@SelectionMode
                                                Selection=@Selection
                                                Compact=@Compact
                                                CheckboxVisibility=@CheckboxVisibility />
                                }
                            </ItemTemplate>
                        </List>
                    }
                    else
                    {
                        <GroupedList ItemsSource=@ItemsSource
                                     TItem="TItem"
                                     TKey="object"
                                     GetKey=@(GetKey!)
                                     @ref=@groupedList
                                     Selection=@Selection
                                     SubGroupSelector=@SubGroupSelector
                                     GroupTitleSelector=@(GroupTitleSelector!)
                                     SelectionMode=@(CheckboxVisibility != CheckboxVisibility.Hidden ? SelectionMode : SelectionMode.None)>
                            <ItemTemplate>
                                <DetailsRow Item=@context.Item!.Item
                                            ItemIndex=@context.Index
                                            TItem="TItem"
                                            Columns=@_adjustedColumns
                                            SelectionMode=@(CheckboxVisibility != CheckboxVisibility.Hidden ? SelectionMode : SelectionMode.None)
                                            Selection=@Selection
                                            CheckboxVisibility=@CheckboxVisibility
                                            Compact=@Compact />
                            </ItemTemplate>
                        </GroupedList>
                    }
                }
            </FocusZone>
        </div>
        @if (FooterTemplate != null)
        {
            @FooterTemplate
        }
    </div>
</div>
